from random import randrange
from flask import Flask, render_template
from flask.json import jsonify
from pyecharts import options as opts
from pyecharts.charts import Line
from config import config


app = Flask(__name__, static_folder='templates')


def line_base() -> Line:
    """
    全量加载
    :return:
    """
    c = (
        Line()
        .add_xaxis(["{}".format(i) for i in range(10)])
        .add_yaxis(series_name='', y_axis=[randrange(50, 80) for _ in range(10)],
                   is_smooth=True, label_opts=opts.LabelOpts(is_show=False))

        .set_global_opts(title_opts=opts.TitleOpts(title='动态数据', subtitle='副标题'),
                         xaxis_opts=opts.AxisOpts(type_='value'),
                         yaxis_opts=opts.AxisOpts(type_='value'))
    )
    return c


@app.route('/')
def index():
    return render_template('index3.html')


@app.route('/lineChart')
def get_bar_chart():
    c = line_base()
    return c.dump_options_with_quotes()


idx = 9


@app.route('/linedynamic')
def update_line_data():
    global idx
    idx += 1
    return jsonify({'name': idx, 'value': randrange(50, 80)})


if __name__ == '__main__':
    app.run(debug=True)
